/******************************************************************************
** 
** File name: 	UTEST_54.c
**     
** Description: RTOS Ver.3 Unit Tests. Sending messages from tasks and ISRs of
**				various levels to the single message queue. Waiting of message
**				acknowledgement added compare to the UTEST_53.
**
**  RTOS API Services to be checked:
**
**				- RTOS_WaitMessage()
**            	- RTOS_SendMessage()
**				- RTOS_SendMessageISR()
**				- RTOS_WaitMessageAck()
**				- RTOS_AckMessage()
**
** Notes:		This is MANUAL test!
**
*******************************************************************************
**
** Change history:
**	
** 09/25/2012 -AK- 	Introducing new unit test standard with RTOS resources
**					configuration moved to the dedicated source/header file
**
******************************************************************************/
#include <avr/io.h>
#include <avr/interrupt.h>

#ifndef UTEST_54
#	error "UTEST_54 must be defined as -D$(Configuration) compiler option"
#endif

#include "UTEST_RES.h"

/******************************************************************************
** UNIT TEST INTERRUPT FUNCTION[s]
******************************************************************************/
/*
** Timer #2 Handler
*/

RTOS_ISR_FUNC(TIMER_2_Handler)
{
	static uint16_t cnt1 = 0;

	RTOS_DebugLogDump();

	RTOS_DebugPrint("[$2] Sending message #1:%04X",cnt1);

	RTOS_PutMessageData(&msg1, cnt1, uint16_t);
	RTOS_SendMessageISR(&msq1, &msg1);

	RTOS_DebugLogDump();

	cnt1++;
}

/*
** Timer #1 Handler
*/
RTOS_ISR_FUNC(TIMER_1_Handler)
{
	static uint16_t cnt0 = 0;
	
	RTOS_DebugLogDump();
	
	RTOS_DebugPrint("[$1] Sending message #0:%04X",cnt0);
	
	RTOS_PutMessageData(&msg0, cnt0, uint16_t);
	RTOS_SendMessageISR(&msq1, &msg0);

	RTOS_DebugLogDump();

	cnt0++;
}

/******************************************************************************
** UNIT TEST TASK FUNCTION[s]
******************************************************************************/
/*
** RTOS Task #0 - Stack checking task
*/
RTOS_TASK_FUNC(tsk0)
{
	while (1)
	{		
		RTOS_DebugLog("[T0] Check Stack");
	
		RTOS_StackCheck(tsk0);
		RTOS_StackCheck(tsk1);
		RTOS_StackCheck(tsk2);
		RTOS_StackCheck(tsk3);
				
		RTOS_TaskSuspend(&tsk0PID);
	}
}

/*
** RTOS Task #1
*/
RTOS_TASK_FUNC(tsk1)
{
	RTOS_MSG_ptr 	msg; 
	uint16_t		data;
	
	while (1)
	{
		RTOS_StackCheck(tsk1);
		RTOS_DebugPrint("\t[T1] blocks on wait ..."); 

		msg = RTOS_WaitMessage(&msq1);
		
		if 		(msg == &msg0)
		{
			data = RTOS_GetMessageData(&msg0, uint16_t);
			RTOS_DebugPrint("\t[T1] received message #0:%04X", data);                                       
		}
		else if (msg == &msg1)
		{
			data = RTOS_GetMessageData(&msg1, uint16_t);
			RTOS_DebugPrint("\t[T1] received message #1:%04X", data);                                       
		}
		else if (msg == &msg2)
		{
			data = RTOS_GetMessageData(&msg2, uint16_t);
			RTOS_DebugPrint("\t[T1] received message #2:%04X", data);       
			
			RTOS_AckMessage(&msg3);
		}
		else if (msg == &msg3)
		{
			data = RTOS_GetMessageData(&msg3, uint16_t);
			RTOS_DebugPrint("\t[T1] received message #3:%04X", data);                                       
			
			RTOS_AckMessage(&msg2);
		}
		else
		{
			RTOS_DebugLog("\t[T1] ERROR::unknown message received!!!");       			 
		}
	}
}

/*
** RTOS Task #2 
*/
RTOS_TASK_FUNC(tsk2)
{
	static uint16_t cnt2 = 0;

	while (1)
	{
		RTOS_StackCheck(tsk2);
		RTOS_DebugPrint("[T2] Sending message #2:%04X", cnt2);       
		RTOS_PutMessageData(&msg2, cnt2, uint16_t);
		RTOS_SendMessage(&msq1,&msg2);	
		RTOS_DebugPrint("[T2] blocks on wait MsgACK...");       		
		RTOS_WaitMessageAck(&msg2);
		cnt2++;
	}
}

/*
** RTOS Task #3 
*/
RTOS_TASK_FUNC(tsk3)
{
	static uint16_t cnt3;

	while (1)
	{
		RTOS_StackCheck(tsk3);
		RTOS_DebugPrint("[T3] Sending message #3:%04X", cnt3);       
		RTOS_PutMessageData(&msg3, cnt3, uint16_t);
		RTOS_SendMessage(&msq1,&msg3);	
		RTOS_DebugPrint("[T3] blocks on wait MsgACK...");       		
		RTOS_WaitMessageAck(&msg3);
		cnt3++;
	}
}
